package com.chinasoft04.mall.controller;



import com.chinasoft04.mall.dto.Good;
import com.chinasoft04.mall.dto.Goods;
import com.chinasoft04.mall.dto.Rbody;
import com.chinasoft04.mall.service.GoodsService;
import com.chinasoft04.mall.dto.*;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.im4java.core.IM4JavaException;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@AllArgsConstructor
@RequestMapping("/api/private/v1")
public class GoodsController {

    GoodsService goodsService;

    @GetMapping("/goods")
    public Rbody getGoods(String query, Integer pagenum, Integer pagesize){
        Rbody rbody = new Rbody();

        Map<String, Object> data = new HashMap<>();
        List<Goods> goodsList = goodsService.getGoods(query, pagenum,pagesize);
        PageInfo<Goods> info = new PageInfo<>(goodsList);
        data.put("total",info.getTotal());
        data.put("pagenum",String.valueOf(info.getPageNum()));
        data.put("goods",goodsList);

        rbody.getMeta().setMsg("获取成功！");
        rbody.setData(data);
        return rbody;
    }



    //根据ID查询商品
    @GetMapping("/goods/{id}")
    public Rbody getGood(@PathVariable Integer id){
        Rbody rbody = new Rbody();



        rbody.setData(goodsService.getGood(id));

        rbody.getMeta().setMsg("获取成功");
        rbody.getMeta().setStatus(201);
        return rbody;
    }


    //编辑提交商品
    @PutMapping("/goods/{id}")
    public Rbody updateGood(@PathVariable Integer id, Good good){
        Rbody rbody = new Rbody();

        if(!goodsService.updateGood(id,good)){
            rbody.getMeta().setStatus(111);
            rbody.getMeta().setMsg("编辑失败");



        }

        rbody.setData(goodsService.getGood(id));

        rbody.getMeta().setMsg("创建商品成功");
        rbody.getMeta().setStatus(201);

        return rbody;
    }

    @DeleteMapping("/goods/{id}")
    public Rbody deleteGood(@PathVariable Integer id){
        Rbody rbody = new Rbody();


        goodsService.deleteGood(id);
        rbody.getMeta().setStatus(200);
        rbody.getMeta().setMsg("删除成功");


        return rbody;
    }

    @PostMapping("/goods")
    public Rbody addGood(@RequestBody Good good, HttpServletRequest request) throws IOException, InterruptedException, IM4JavaException {
        Rbody rbody = new Rbody();



        String realPath = request.getSession().getServletContext().getRealPath("/");


        String[] cat = good.getGoods_cat().split(",");
        good.setCat_one_id(Integer.parseInt(cat[0]));
        good.setCat_two_id(Integer.parseInt(cat[1]));
        good.setCat_three_id(Integer.parseInt(cat[2]));

        goodsService.addGood(good,realPath);




        rbody.setData("我在这里怎么写都没问题吧");
        rbody.getMeta().setMsg("创建商品成功");
        rbody.getMeta().setStatus(201);

        return rbody;
    }

}
